const ajax = options => {
    const {
        type,
        dataType = 'json',
        path,
        success,
        // 字对象
        data = {}
    } = options;


    // 把data这个对象变成  
    //   {username:'yy',password:'123'}   =>  username=yy&password=123
    let params = '';
    for (let key in data) {
        params += `${key}=${data[key]}&`;
    }

    params = params.substring(0, params.length - 1);


    const xhr = new XMLHttpRequest();

    // 判断请求方式

    if (type.toLowerCase() === 'get') {
        xhr.open(type, path + '?' + params, true);
        xhr.send();
    }
    else if (type.toLowerCase() === 'post') {
        xhr.open(type, path, true);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let data = xhr.responseText;
            if (dataType.toLowerCase() === 'json') {
                data = JSON.parse(data);
            }
            success(data);
        }
    }
}